-- card: 6547 from stack: in -- bmap block id: 0 -- flags: 0000 -- background id: 4755 -- name: -- part contents for background part 6 ----- text ----- 9.3 Class Ideas -- part contents for background part 4 ----- text ----- C++ provides "constructor" and "destructor" member functions which are automatic- ally invoked upon allocation and deallocation of objects. Typically the constructor initializes the values of the instance variables and/or performs dynamic memory allocation, while the destructor deallocates any dynamically-allocated memory. This section extends the Generic_Class files given in the example in Chapter 4 and suggests new global functions new_init() and destroy_delete() to supplant the TC new() and delete() functions defined in the oops library. These alternative functions automatically invoke an object's init() and destroy() member functions to simulate C++ constructors and destructors. They also work for both direct and indirect objects*. Unlike the C++ constructor/destructor approach, however, this technique has the potential to ensure that any dynamic memory allocation in the init() method succeeds before an object pointer is assigned. That is, new_init() returns either: (1) NULL if the object cannot be allocated, (2) NULL if the object is allocated but its init() method fails (in which case all allocated memory is released), or (3) a pointer to a successfully-initialized object. IMPORTANT: the code on the following pages must be modified to work in C++, as discussed in the comments. -- part contents for background part 29 ----- text ----- 38494 -- part contents for background part 27 ----- text ----- Direct & indirect objects -- part contents for background part 20 ----- text ----- Direct & indirect objects - p123 -- part contents for background part 7 ----- text ----- 209 -- part contents for background part 17 ----- text ----- p3